package com.authentication.center.system.oauth2;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : garvin
 * @version V1.0
 * @date : 2020年10月19日 13:57
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    private Map<String, Object> additionalInformation;

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        if (getAdditionalInformation().size() > 0) {
            accessToken.getAdditionalInformation().putAll(additionalInformation);
        }
        return accessToken;
    }

    public Map<String, Object> getAdditionalInformation() {
        if (Objects.isNull(additionalInformation)) {
            additionalInformation = new LinkedHashMap<>();
        }
        return additionalInformation;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = additionalInformation;
    }
}
